﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Honestysoft.Financing.WebUI.Module.Reports.JSON
{
    /// <summary>
    /// GetR011 的摘要说明
    /// </summary>
    public class GetR011 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            ////复制模板到新目录，并且重写数据，刷新图表
            //string date = context.Request.Params["Date"];
            //DataTable dt = new Honestysoft.Financing.BusinessLogic.Reports("").GetR011(Convert.ToDateTime(date));
            //Template.JDZF jdFile = new Template.JDZF(date + ".xls");
            //jdFile.InitTargetDir();
            //jdFile.DeleteTargetFile();
            //Honestysoft.BaseLibrary.Common.ExcelHelper excelHelper = new BaseLibrary.Common.ExcelHelper(jdFile.TemplateFileMapPath, jdFile.TargetFileMapPath);
            //try
            //{
            //    excelHelper.DataTableToExcel(dt, 2, 1, excelHelper.GetSheet(2));
            //    excelHelper.SetChartDataSource(1, "图表 2", 2, "A1", "E" + (1 + dt.Rows.Count).ToString());
            //    excelHelper.OutputExcelFile();
            //    context.Response.Write("1");
            //}
            //catch
            //{
            //    context.Response.Write("0");
            //}
            //finally
            //{
            //    excelHelper.Quit();
            //}


            context.Response.ContentType = "text/plain";
            //复制模板到新目录，并且重写数据，刷新图表
            string date = context.Request.Params["Date"];
            DateTime theDate = Convert.ToDateTime(date);
            BusinessLogic.Reports logic = new BusinessLogic.Reports("");
            //DataTable dt1 = logic.GetR010_1(theDate);
            DataTable dt2 = logic.GetR011(theDate);
            DataTable dtJD = dt2.Copy();
            DataTable dtDB = dt2.Copy();
            DataTable dtChart = new DataTable();
            dtJD.Columns.Remove("ProjectID");
            dtJD.Columns.Remove("vSort");
            dtJD.Columns.Remove("FCode");
            dtJD.Columns.Remove("P_TZ");
            dtJD.Columns.Remove("P_JDZF");
            dtJD.Columns.Remove("P_ZBJ");
            dtJD.Columns.Remove("ZBJ");
            dtJD.Columns.Remove("GS");
            dtJD.Columns.Remove("AmountS");

            dtDB.Columns.Remove("ProjectID");
            dtDB.Columns.Remove("vSort");
            dtDB.Columns.Remove("FCode");
            dtDB.Columns.Remove("P_TZ");
            dtDB.Columns.Remove("P_JDZF");
            dtDB.Columns.Remove("P_ZBJ");
            dtDB.Columns.Remove("KcDate");
            dtDB.Columns.Remove("PlanStartDate");
            dtDB.Columns.Remove("PlanEndDate");
            dtDB.Columns.Remove("StartDate");
            dtDB.Columns.Remove("EndDate");
            dtDB.Columns.Remove("JD");

            dtChart.Columns.Add("SN", typeof(string));
            dtChart.Columns.Add("FCode", typeof(string));
            dtChart.Columns.Add("ProjectName", typeof(string));
            dtChart.Columns.Add("P_JDZF", typeof(string));
            dtChart.Columns.Add("P_TZ", typeof(string));
            dtChart.Columns.Add("P_ZBJ", typeof(string));
            dtChart.Columns.Add("G", typeof(string));
            dtChart.Columns.Add("H", typeof(string));
            dtChart.Columns.Add("GS", typeof(decimal));
            dtChart.Columns.Add("JD", typeof(decimal));
            dtChart.Columns.Add("ZBJ", typeof(decimal));
            dtChart.Columns.Add("AmountS", typeof(decimal));
            DataView dvSort = new DataView(dt2);
            dvSort.Sort = "GS";
            DataTable dt3 = dvSort.ToTable();

            DataRow newRow = null;
            foreach (DataRow dr in dt3.Rows)
            {
                newRow = dtChart.NewRow();
                newRow["SN"] = dr["SN"].ToString();
                newRow["FCode"] = dr["FCode"].ToString();
                newRow["ProjectName"] = dr["ProjectName"].ToString();
                newRow["P_JDZF"] = dr["P_JDZF"];
                newRow["P_TZ"] = dr["P_TZ"];
                newRow["P_ZBJ"] = dr["P_ZBJ"];
                newRow["G"] = dr["FCode"] + " " + dr["ProjectName"] + " " + string.Format("{0:P}", dr["P_JDZF"]).PadLeft(6, ' ') + " " + string.Format("{0:P}", dr["P_TZ"]).PadLeft(6, ' ') + " " + string.Format("{0:P}", dr["P_ZBJ"]).PadLeft(6, ' ');
                newRow["H"] = newRow["G"];
                newRow["GS"] = dr["GS"];
                newRow["JD"] = dr["JD"];
                dtChart.Rows.Add(newRow);
                newRow = dtChart.NewRow();
                newRow["SN"] = "";
                newRow["ZBJ"] = dr["ZBJ"];
                newRow["AmountS"] = dr["AmountS"];
                dtChart.Rows.Add(newRow);
                newRow = dtChart.NewRow();
                newRow["SN"] = "";
                dtChart.Rows.Add(newRow);
            }

            Template.JDZF jdFile = new Template.JDZF(date + ".xls");
            jdFile.InitTargetDir();
            jdFile.DeleteTargetFile();
            Honestysoft.BaseLibrary.Common.ExcelHelper excelHelper = new BaseLibrary.Common.ExcelHelper(jdFile.TemplateFileMapPath, jdFile.TargetFileMapPath);
            try
            {
                excelHelper.DataTableToExcel(dtJD, 2, 1, excelHelper.GetSheet(3));
                excelHelper.DataTableToExcel(dtDB, 2, 1, excelHelper.GetSheet(2));
                excelHelper.DataTableToExcel(dtChart, 2, 1, excelHelper.GetSheet(1));
                //excelHelper.SetChartDataSource(1, "图表 1", 2, "B1", "E" + (1 + dt.Rows.Count).ToString());
                excelHelper.SetChartDataSource(1, "图表 12", 1, "H1", "L" + (1 + dtChart.Rows.Count).ToString());
                Microsoft.Office.Interop.Excel.ChartObject cobj = (Microsoft.Office.Interop.Excel.ChartObject)excelHelper.GetSheet(1).ChartObjects("图表 12");
                cobj.Chart.ChartTitle.Text = theDate.ToString("yyyy年M月") + "项目投资计划完成情况及工程进度和资金拨付比例示意图";
                excelHelper.SetTextBox(1, "TextBox 2", theDate.ToString("yyyy.M.d") + "绘制");
                excelHelper.OutputExcelFile();
                context.Response.Write("1");
            }
            catch
            {
                context.Response.Write("0");
            }
            finally
            {
                excelHelper.Quit();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}